package com.htjx.mall.controllers;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htjx.mall.commons.api.Result;
import com.htjx.mall.commons.models.Addressee;
import com.htjx.mall.commons.models.User;
import com.htjx.mall.commons.utils.Checks;
import com.htjx.mall.controllers.commonapi.AuthorizeList;
import com.htjx.mall.services.AddresseeService;
import com.htjx.mall.services.UserService;

@Controller
@RequestMapping("/addressee")
public class AddresseeController {
	@Autowired
	private AddresseeService addresseeService;
	@Autowired
	private UserService userService;
	
	/****
	 * 查询收件人list
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result list(HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"查询失败");
		}
		
		List<Addressee> addresseeList = addresseeService.selectByUserId(user.getId());
		
		Result result = new Result(0, "查询成功");
		result.setBody(addresseeList);
		return result;
	}
	
	/****
	 * 选择首选地址
	 */
	@RequestMapping(value = "/prime", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result prime(HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"查询失败");
		}
		
		Addressee addressee = addresseeService.selectById(user.getAddressee_id());
		
		Result result = new Result(0, "查询成功");
		result.setBody(addressee);
		return result;
	}
	
	/****
	 * 设置首选地址
	 */
	@RequestMapping(value = "/prime", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result prime(long id, HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"查询失败");
		}
		
		Addressee addressee = addresseeService.selectById(id);
		if(addressee == null) {
			return new Result(9,"查询失败.");
		}
		
		user.setAddressee_id(id);
		userService.updateOne(user);
		
		Result result = new Result(0, "成功");
		return result;
	}
	
	/****
	 * 添加收件人
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result add(Addressee addressee, boolean prime, HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"请登录后到用户中心管理地址");
		}
		
		String rec_name = addressee.getRec_name();
		if (StringUtils.isBlank(rec_name)) {
			return new Result(9,"收件人不能为空");
		}
		
		String phone = addressee.getPhone();
		if (StringUtils.isBlank(phone)) {
			return new Result(9,"手机不能为空");
		}
		
		if(!Checks.isMobileNO(phone)) {
			return new Result(9, "请输入正确的手机号码");
		}
		
		String province = addressee.getProvince();
		if (StringUtils.isBlank(province)) {
			return new Result(9,"请选择省份/直辖市");
		}
		
		String city = addressee.getCity();
		if (StringUtils.isBlank(city)) {
			return new Result(9,"请选择城市");
		}
		
		String area = addressee.getArea();
		if (StringUtils.isBlank(area)) {
			return new Result(9,"请选择区/县/市");
		}
		
		String address = addressee.getAddress();
		if (StringUtils.isBlank(address)) {
			return new Result(9,"收件地址不能为空");
		}
		
		addressee.setUser_id(user.getId());
		addressee.setActive(true);
		addressee.setCreate_time(new Date());
		addresseeService.insertOne(addressee, user, prime);
		
		Result result = new Result(0, "添加成功");
		return result;
	}
	
	/****
	 * 修改收件人
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result update(Addressee addressee, boolean prime, HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"请登录后到用户中心管理地址");
		}
		
		String rec_name = addressee.getRec_name();
		if (StringUtils.isBlank(rec_name)) {
			return new Result(9,"收件人不能为空");
		}
		
		String phone = addressee.getPhone();
		if (StringUtils.isBlank(phone)) {
			return new Result(9,"手机不能为空");
		}
		
		if(!Checks.isMobileNO(phone)) {
			return new Result(9, "请输入正确的手机号码");
		}
		
		String address = addressee.getAddress();
		if (StringUtils.isBlank(address)) {
			return new Result(9,"收件地址不能为空");
		}
		
		addressee.setUser_id(user.getId());
		addressee.setActive(true);
		addresseeService.updateOne(addressee, user, prime);
		
		Result result = new Result(0, "更新成功");
		return result;
	}
	
	/****
	 * 删除收件人
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public Result delete(long id, HttpServletRequest request) {
		long userId = AuthorizeList.getCurrentAuthUser(request);
		User user = userService.findUniqueBy("id", String.valueOf(userId));
		if(user == null) {
			return new Result(9,"请登录后到用户中心管理地址");
		}
		addresseeService.deleteOne(id, user);
		
		Result result = new Result(0, "删除成功");
		return result;
	}
	
}
